<template>
	<view class="container">
		<uni-section title="公司信息" type="line">
			<view class="example">
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms ref="baseForm" :model="baseFormData" labelWidth="80px">
					<uni-forms-item label="名称">
						<uni-easyinput v-model="baseFormData.company_name" :value="baseFormData.company_name" placeholder="请填写公司名称" />
					</uni-forms-item>
					<uni-forms-item label="电话">
						<uni-easyinput v-model="baseFormData.phone" :value="baseFormData.phone" placeholder="请填写联系电话" />
					</uni-forms-item>
					<uni-forms-item label="手机">
						<uni-easyinput v-model="baseFormData.mobile_phone" :value="baseFormData.mobile_phone" placeholder="请填写联系人手机" />
					</uni-forms-item>
					<uni-forms-item label="传真">
						<uni-easyinput v-model="baseFormData.fax" :value="baseFormData.fax" placeholder="请填写公司传真" />
					</uni-forms-item>
					<uni-forms-item label="地址">
						<uni-easyinput v-model="baseFormData.address" :value="baseFormData.address" placeholder="请填写公司地址" />
					</uni-forms-item>
				</uni-forms>
				<button type="primary" @click="submit('valiForm')">保存</button>
			</view>
		</uni-section>

	</view>
</template>

<script>
	import {updateCompany} from '@/common/api'
	export default {
		data() {
			return {
				// 基础表单数据
				baseFormData: {
					company_name: '',
					phone: '',
					mobile_phone: '',
					fax: '',
					address: '',
				},
				user_id:0,
			}
		},
		
		onLoad() {
			let userInfo = uni.getStorageSync('userInfo');
			if(!userInfo){
				uni.showToast({
					title: '登录失效,重新登录',//标题 必填
					icon: 'none',//图标
				    mask: false,//是否显示透明蒙层，防止触摸穿透，默认：false
				    duration: 2000,//提示的延迟时间，单位毫秒，默认：1500
				});
				setTimeout(function () {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				}, 2000);
			}
			let companyInfo = uni.getStorageSync('companyInfo');
			companyInfo = JSON.parse(companyInfo);
			if(companyInfo){
				// this.custom_id = companyInfo.custom_id
				this.baseFormData = companyInfo;
			}else{
				this.custom_id = 0;
			}
			userInfo = JSON.parse(userInfo);
			// this.userinfo = userInfo;
			this.user_id = userInfo.user_id;
		},
		onReady() {
			// 设置自定义表单校验规则，必须在节点渲染完毕后执行
			// this.$refs.customForm.setRules(this.customRules)
		},
		methods: {
			submit(ref) {
				uni.showLoading({
					title: '...',
					mask: false
				});
				let hash = 'acbcefg';
				let timestamp = Math.round(new Date().getTime() / 1000).toString();
				
				let user_id = this.user_id;
				// let custom_id = this.custom_id;
				let company_name = this.baseFormData.company_name;
				let phone = this.baseFormData.phone;
				let mobile_phone = this.baseFormData.mobile_phone;
				let fax = this.baseFormData.fax;
				let address = this.baseFormData.address;
				updateCompany({
					// custom_id:custom_id,
					user_id:user_id,
					company_name:company_name,
					phone:phone,
					mobile_phone:mobile_phone,
					fax:fax,
					address:address,
					hash:hash,
					timestamp:timestamp,
					}).then(res=>{
					console.log(res);
					if(res.code == '200'){
						uni.showToast({
							title: "保存成功",
							 duration: 1500,//提示的延迟时间，单位毫秒，默认：1500
						})
						
						this.baseFormData = res.data;
						uni.setStorageSync('companyInfo',JSON.stringify(res.data));
						setTimeout(function () {
							uni.hideLoading();
							uni.switchTab({
								url: '/pages/index/user'
							});
						}, 1500);
						
					}else{
						uni.showToast({
							title: res.msg,//标题 必填
							icon: 'none',//图标
						    mask: false,//是否显示透明蒙层，防止触摸穿透，默认：false
						    duration: 2000,//提示的延迟时间，单位毫秒，默认：1500
						});
						return
					}
				}).catch(
					(response) => {
						uni.showToast({
							title: "系统错误"+response.message,//标题 必填
							icon: 'none',//图标
						    mask: false,//是否显示透明蒙层，防止触摸穿透，默认：false
						    duration: 2000,//提示的延迟时间，单位毫秒，默认：1500
						});
					}
				)
				// this.$refs[ref].validate().then(res => {
					
				// }).catch(err => {
				// 	console.log('err', err);
				// })
			},
		}
	}
</script>

<style lang="scss">
	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>
